package edu.noteBook.controller;


import edu.noteBook.Pojo.Note;
import edu.noteBook.Pojo.User;
import edu.noteBook.service.UserService;
import edu.noteBook.utils.JwtUtil;
import edu.noteBook.utils.R;
import edu.noteBook.service.NoteService;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/note")
@Validated
@CrossOrigin
public class NoteController {

    @Autowired
    private NoteService noteService;

    @Autowired
    private UserService userService;


    /**
     * 查询所有笔记
     * @param current
     * @param size
     * @return noteService.queryAllNote(current,size)
     */
    @GetMapping("/{current}/{size}")
    public R queryAllNote(@PathVariable("current") Integer current, @PathVariable("size") Integer size) {
        System.out.println(noteService.queryAllNote(current,size));
        return new R(true, noteService.queryAllNote(current,size));
    }

    /**
     * 查询我的笔记
     * @param current
     * @param size
     * @param authHeader
     * @return noteService.queryAllNote(current,size)
     */
    @GetMapping("/user/{current}/{size}")
    public R queryMyNote(@PathVariable("current") Integer current, @PathVariable("size") Integer size,@RequestHeader("Authorization") String authHeader) {
        Integer username;
        try {
//            System.out.println("收到的Authorization header: " + authHeader);
            if (authHeader == null || !authHeader.startsWith("Bearer ")) {
                System.out.println("Authorization header格式错误");
                return new R(false, null, "无效的token格式");
            }
            String token = authHeader.substring(7);
//            System.out.println("提取的token: " + token);
            // 先验证token
            if (!JwtUtil.validateToken(token)) {
                System.out.println("token验证失败");
                return new R(false, null, "无效的token");
            }
            Claims claims = JwtUtil.parseToken(token);
//            System.out.println("解析后的claims: " + claims);
            Integer userId = claims.get("id", Integer.class);
            System.out.println("从token中获取的用户ID: " + userId);
            if (userId == null) {
//                System.out.println("token中没有用户ID");
                return new R(false, null, "token中无用户信息");
            }
            User currentUser = userService.queryUserByID(userId);
//            System.out.println("查询到的用户信息: " + currentUser);
            if (currentUser == null) {
                System.out.println("未找到用户信息");
                return new R(false, null, "未找到用户信息");
            }
            username = currentUser.getId();
        } catch (Exception e) {
            System.out.println("处理getCurrentUser请求时发生错误: " + e.getMessage());
            e.printStackTrace();
            return new R(false, null, "获取用户信息失败: " + e.getMessage());
        }
//        return new R(true, noteService.queryAllNote(current,size));
        return new R(true, noteService.queryAllNote1(current, size, username));
    }

    /**
     * 根据id查询笔记
     * @param id
     * @return noteService.queryNoteById(id)
     */
    @GetMapping("/{id}")
    public R queryNoteById(@PathVariable Integer id) {
        return new R(true, noteService.queryNoteById(id));
    }

    /**
     * 根据标题查询笔记
     * @param title
     * @return noteService.queryNoteByTitle(title)
     */
    @GetMapping("/title/{title}")
    public R queryNoteByTitle(@PathVariable("title") String title) {
        return new R(true, noteService.queryNoteByTitle(title));
    }

    /**
     * 根据类型查询笔记
     * @param type
     * @return noteService.queryNoteByType(type)
     */
    @GetMapping("/type/{type}")
    public R queryNoteByType(@PathVariable("type") Integer type) {
        return new R(true, noteService.queryNoteByType(type));
    }

    /**
     * 删除笔记
     * @param id
     * @return noteService.deleteNote(id)
     */
    @DeleteMapping("/{id}")
    public R deleteNote(@PathVariable("id") Integer id) {
        return new R(noteService.deleteNote(id));
    }

    /**
     * 添加笔记
     * @param note
     * @return noteService.addNote(note)
     */
    @PostMapping
    public R addNote(@RequestBody @Validated Note note) {
        System.out.println(note);
        Date date = new Date();
        note.setCreateTime(date);
        return new R(noteService.addNote(note));
    }


    /**
     * 修改笔记
     * @param note
     * @return noteService.updateNote(note)
     */
    @PutMapping
    public R updateNote(@RequestBody @Validated Note note) {
        System.out.println(note.getId());
        Date date = new Date();
        note.setUpdateTime(date);
        return new R(noteService.updateNote(note));
    }
}